package com.story.api.moduler.song.controller;

import com.story.api.common.result.ServerResponse;
import com.story.api.moduler.song.aop.PlayCount;
import com.story.api.moduler.song.aop.UseApp;
import com.story.api.moduler.song.service.SongService;
import com.story.api.moduler.upyun.service.UpyunService;
import com.story.core.util.UriUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @decription 歌曲接口
 * @Date 2017-12-11 10:34
 */
@RestController
@RequestMapping(value = "/child")
public class SongController {

    private static final Logger log = LoggerFactory.getLogger(SongController.class);

    private static final Long EXPIRE_TIME = 1800L;

    @Autowired
    private SongService songService;

    @Autowired
    private UpyunService upyunService;

    /**
     * 得到首页数据
     * @return
     */
    @UseApp
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public ServerResponse getIndexData() {
        return songService.getIndexPage();
    }

    /**
     * 首页年龄段点击进入歌曲列表
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/ageSongPageList",method = RequestMethod.POST)
    public ServerResponse getAgeListByPage(@RequestParam(value = "currentPage",required = false) Integer currentPage,
                                           @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                           @RequestParam(value = "ageType") Integer ageType) {
        if(ageType == null) {
            return ServerResponse.getErrorMessage("请指定年龄范围");
        }
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 6 : pageSize;
        return songService.getAgeSongListByPage(currentPage,pageSize,ageType);
    }

    /**
     * 得到所有的栏目
     * @return
     */
    @RequestMapping(value = "/bannerList",method = RequestMethod.POST)
    public ServerResponse getAllBanner() {
        return songService.getAllBanner();
    }

    /**
     * 得到栏目下的专辑分页
     * @param currentPage
     * @param pageSize
     * @param bannerId
     * @return
     */
    @RequestMapping(value = "/getCatePageByBanner",method = RequestMethod.POST)
    public ServerResponse getCategoryPageListByBannerId(@RequestParam(value = "currentPage",required = false) Integer currentPage,
                                                        @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                        @RequestParam(value = "bannerId") Integer bannerId) {
        if(bannerId == null) {
            return ServerResponse.getErrorMessage("请携带bannerId");
        }
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 6 : pageSize;
        return songService.getCateListPage(currentPage,pageSize,bannerId);
    }

    /**
     * 得到专辑下的视频分页列表
     * @param currentPage
     * @param pageSize
     * @param cateId
     * @return
     */
    @RequestMapping(value = "/getVedioPageByCateId",method = RequestMethod.POST)
    public ServerResponse getVedioDetailPageByCateId(@RequestParam(value = "currentPage",required = false) Integer currentPage,
                                                     @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                     @RequestParam(value = "cateId") Integer cateId) {
        if(cateId == null) {
            return ServerResponse.getErrorMessage("请携带cateId");
        }
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 6 : pageSize;
        return songService.getVedioDetailPage(currentPage,pageSize,cateId);
    }

    /**
     * 得到栏目下的随意两个专辑
     * @param bannerId
     * @return
     */
    @RequestMapping(value = "/guessYouLike",method = RequestMethod.POST)
    public ServerResponse getGuessYouLike(@RequestParam(value = "bannerId",required = false) Integer bannerId) {
        return songService.getGuessYouLikeCate(bannerId);
    }

    /**
     * 热门搜索前十
     * @return
     */
    @RequestMapping(value = "/hotSearch",method = RequestMethod.POST)
    public ServerResponse hotSearch() {
        return songService.getHotVedios(1);
    }

    /**
     * 搜索
     * @param keyword
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/search",method = RequestMethod.POST)
    public ServerResponse getSearchPageList(@RequestParam(value = "keyword",required = false) String keyword,
                                            @RequestParam(value = "currentPage",required = false) Integer currentPage,
                                            @RequestParam(value = "pageSize",required = false) Integer pageSize) {
        if(StringUtils.isBlank(keyword)) {
            return ServerResponse.getErrorMessage("请输入关键词");
        }
        keyword = keyword.trim();
        currentPage = currentPage == null ? 1 : currentPage;
        pageSize = pageSize == null ? 6 : pageSize;
        return songService.getSearchPage(currentPage,pageSize,keyword,1);
    }

    /**
     * 得到播放地址
     * @param playPath
     * @return
     */
    @PlayCount
    @RequestMapping(value = "/playAddress",method = RequestMethod.POST)
    public ServerResponse getPlayAddress(@RequestParam(value = "playPath") String playPath) {
        playPath = playPath + "?_upt=" + upyunService.getTokenSign(UriUtil.getUri(playPath),EXPIRE_TIME);
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("playAddress",playPath);
        return ServerResponse.getSuccess("success",result);
    }

    @RequestMapping(value = "/playAuth",method = RequestMethod.GET)
    public ServerResponse authPlayAddress() {
        //TODO 回源鉴权接口
        return null;
    }
}
